##### Replication File: Political Research Quarterly
##### Jonathan M. King, Jessica A. Schoenherr, and Ian Ostrander 
##### Anchoring or Expanding? Gender and Judicial Nominations
library(stargazer)
library(arm)
library(readr)
library(fastDummies)

#Import Data
KingSchoenherrOstranderPRQ2024 <- read_csv("KingSchoenherrOstranderPRQ2024.csv")
noms=KingSchoenherrOstranderPRQ2024
#separate by court level (circuit=2, district=3)
noms_circuit=subset(noms,noms$courttype==2)
noms_district=subset(noms,noms$courttype==3)



###################################################################################################################
###################################################################################################################
###################################################################################################################
###################################################################################################################
#########
######### Models
#########
###################################################################################################################
###################################################################################################################
###################################################################################################################
###################################################################################################################



#########################################
######### Female Nominee -- Main Model
#########################################
##### Circuit ####
circuit.female=glm(female~PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+PropSensFemale+
                     approval+PresParty+as.factor(congress)+
                     (PrevJgender*PresParty),
                   family=binomial(link='logit'),
                   data=noms_circuit)
summary(circuit.female)


#### District ####
district.female_glm=glm(female~PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+
                          approval+PresParty+as.factor(congress)+as.factor(CourtStateNumeric)+
                          (PrevJgender*PresParty),
                        family=binomial(link='logit'),
                        data=noms_district)
summary(district.female_glm)




###########################################
######### Additional Models- 
###########################################
############## Year FEs ##############
#circuit
circuit.female.year=glm(female~PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+PropSensFemale+
                          approval+PresParty+as.factor(year)+
                          (PrevJgender*PresParty),
                        family=binomial(link='logit'),
                        data=noms_circuit)
summary(circuit.female.year)

#district
district.female.year=glm(female~PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+
                           approval+PresParty+as.factor(year)+as.factor(CourtStateNumeric)+
                           (PrevJgender*PresParty),
                         family=binomial(link='logit'),
                         data=noms_district)
summary(district.female.year)



############## Circuit w/ state FEs ##############
circuit.female.stateFE=glm(female~PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+PropSensFemale+
                             approval+PresParty+as.factor(congress)+as.factor(CourtStateNumeric)+
                             (PrevJgender*PresParty),
                           family=binomial(link='logit'),
                           data=noms_circuit)
summary(circuit.female.stateFE)



############## Circuit 100th Congress Up Only ##############
#Circuit Female Nominee Model 100th and up
noms_circuit_100up=subset(noms_circuit,noms_circuit$congress>=100)
circuit.female.100up=glm(female~PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+PropSensFemale+
                           approval+PresParty+as.factor(congress)+
                           (PrevJgender*PresParty),
                         family=binomial(link='logit'),
                         data=noms_circuit_100up)
summary(circuit.female.100up)

#Circuit Female Nominee Model 100th and up w/ state FEs
noms_circuit_100up=subset(noms_circuit,noms_circuit$congress>=100)
circuit.female.100up.stateFE=glm(female~PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+PropSensFemale+
                                   approval+PresParty+as.factor(congress)+as.factor(CourtStateNumeric)+
                                   (PrevJgender*PresParty),
                                 family=binomial(link='logit'),
                                 data=noms_circuit_100up)
summary(circuit.female.100up.stateFE)



############## Success ##############
#Circuit
circuit.female.success=glm(success~female+PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+as.factor(ABAnum)+BenchPropFemale+PrevNomFail+daysleft+PropSensFemale+
                             approval+election+PresParty+as.factor(congress)+as.factor(CourtStateNumeric)+
                             (PrevJgender*PresParty),
                           family=binomial(link='logit'),
                           data=noms_circuit)
summary(circuit.female.success)

#District
district.female.sucess=glm(success~female+PrevJgender+prevJswitch+DivSen+femalesenator+as.factor(homestateDC3)+as.factor(ABAnum)+BenchPropFemale+PrevNomFail+daysleft+
                             approval+election+PresParty+as.factor(congress)+as.factor(CourtStateNumeric)+
                             (PrevJgender*PresParty),
                           family=binomial(link='logit'),
                           data=noms_district)
summary(district.female.sucess)



############## Alternative Operationalizations of Control Variables ##############
#### Circuit ####
circuit.female.alt=glm(female~PrevJgender+prevJswitch+SenDiff+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+PropSensFemale+
                        rcnomdate+approval+PresParty+as.factor(congress)+
                        (PrevJgender*PresParty),
                      family=binomial(link='logit'),
                      data=noms_circuit)
summary(circuit.female.alt)


#### District ####
district.female_glm.alt=glm(female~PrevJgender+prevJswitch+SenDiff+femalesenator+as.factor(homestateDC3)+BenchPropFemale+PrevNomFail+daysleft+
                             rcnomdate+approval+PresParty+as.factor(congress)+as.factor(CourtStateNumeric)+
                             (PrevJgender*PresParty),
                           family=binomial(link='logit'),
                           data=noms_district)
summary(district.female_glm.alt)



###################################################################################################################
###################################################################################################################
###################################################################################################################
###################################################################################################################
#########
######### Simulations
#########
###################################################################################################################
###################################################################################################################
###################################################################################################################
###################################################################################################################
##### Circuit ####
#data set up
n.draws=10000
sim.circuit.female=coef(sim(circuit.female,n.draws))
sim.circuit.female=as.data.frame(sim.circuit.female)

data.x.circuit.female=subset(noms_circuit,select=c('PrevJgender','prevJswitch','DivSen','femalesenator',
                                                   'BenchPropFemale','PrevNomFail',"daysleft",'PropSensFemale','approval','PresParty','homestateDC3','congress',
                                                   'CourtStateNumeric'))
data.x.circuit.female=na.omit(data.x.circuit.female) 

#create dummy variables for fixed effects
data.x.circuit.female.new=dummy_cols(data.x.circuit.female,select_columns=c('homestateDC3','congress','CourtStateNumeric'))
data.x.circuit.female.new=data.x.circuit.female.new[,-c(11:14,17,38)] #remove duplicate columns

#vote sequence
seq.vote=seq(from=0,to=1)


#set up prediction matrices
circuit.dem.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
circuit.dem.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
circuit.dem.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

circuit.dem.nonprevw.mean=as.matrix(NA,nrow=length(seq.vote))
circuit.dem.nonprevw.hi=as.matrix(NA,nrow=length(seq.vote))
circuit.dem.nonprevw.lo=as.matrix(NA,nrow=length(seq.vote))

circuit.rep.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
circuit.rep.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
circuit.rep.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

circuit.rep.nonprevw.mean=as.matrix(NA,nrow=length(seq.vote))
circuit.rep.nonprevw.hi=as.matrix(NA,nrow=length(seq.vote))
circuit.rep.nonprevw.lo=as.matrix(NA,nrow=length(seq.vote))

diff.circuit.dem.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.dem.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.dem.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

diff.circuit.rep.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.rep.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.rep.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

diff.circuit.dem.rep.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.dem.rep.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.dem.rep.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

diff.circuit.dem.rep.nonprevw.mean=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.dem.rep.nonprevw.hi=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.dem.rep.nonprevw.lo=as.matrix(NA,nrow=length(seq.vote))


#simulations
for(i in 1:length(seq.vote)){
  coa.pred.dem.prevw=cbind(1,
                           1, #judge prev woman
                           data.x.circuit.female.new$prevJswitch,
                           data.x.circuit.female.new$DivSen,
                           data.x.circuit.female.new$femalesenator,
                           data.x.circuit.female.new[,11:12], #home state senator FEs
                           data.x.circuit.female.new$BenchPropFemale,
                           data.x.circuit.female.new$PrevNomFail,
                           data.x.circuit.female.new$daysleft,
                           data.x.circuit.female.new$PropSensFemale,
                           data.x.circuit.female.new$approval,
                           0, #dempres
                           data.x.circuit.female.new[,13:32], #congress FEs
                           0 #prev woman dempres int
  )
  coa.pred.dem.prevw=as.matrix(coa.pred.dem.prevw)
  
  coa.pred.dem.prevnonw=cbind(1,
                              0, #judge prev man
                              data.x.circuit.female.new$prevJswitch,
                              data.x.circuit.female.new$DivSen,
                              data.x.circuit.female.new$femalesenator,
                              data.x.circuit.female.new[,11:12], #home state senator FEs
                              data.x.circuit.female.new$BenchPropFemale,
                              data.x.circuit.female.new$PrevNomFail,
                              data.x.circuit.female.new$daysleft,
                              data.x.circuit.female.new$PropSensFemale,
                              data.x.circuit.female.new$approval,
                              0, #dempres
                              data.x.circuit.female.new[,13:32], #congress FEs
                              0 #prev man dempres int
  )
  coa.pred.dem.prevnonw=as.matrix(coa.pred.dem.prevnonw)
  
  coa.pred.rep.prevw=cbind(1,
                           1, #judge prev woman
                           data.x.circuit.female.new$prevJswitch,
                           data.x.circuit.female.new$DivSen,
                           data.x.circuit.female.new$femalesenator,
                           data.x.circuit.female.new[,11:12], #home state senator FEs
                           data.x.circuit.female.new$BenchPropFemale,
                           data.x.circuit.female.new$PrevNomFail,
                           data.x.circuit.female.new$daysleft,
                           data.x.circuit.female.new$PropSensFemale,
                           data.x.circuit.female.new$approval,
                           1, #reppres
                           data.x.circuit.female.new[,13:32], #congress FEs
                           1 #prev woman reppres int
  )
  coa.pred.rep.prevw=as.matrix(coa.pred.rep.prevw)
  
  coa.pred.rep.prevnonw=cbind(1,
                              0, #judge prev man
                              data.x.circuit.female.new$prevJswitch,
                              data.x.circuit.female.new$DivSen,
                              data.x.circuit.female.new$femalesenator,
                              data.x.circuit.female.new[,11:12], #home state senator FEs
                              data.x.circuit.female.new$BenchPropFemale,
                              data.x.circuit.female.new$PrevNomFail,
                              data.x.circuit.female.new$daysleft,
                              data.x.circuit.female.new$PropSensFemale,
                              data.x.circuit.female.new$approval,
                              1, #reppres
                              data.x.circuit.female.new[,13:32],#congress FEs
                              0 #prev man reppres int
  )
  coa.pred.rep.prevnonw=as.matrix(coa.pred.rep.prevnonw)
  
  #set up predicted probability matrices
  pp.dem.prevw=matrix(NA,nrow=n.draws)
  pp.dem.prevnonw=matrix(NA,nrow=n.draws)
  pp.rep.prevw=matrix(NA,nrow=n.draws)
  pp.rep.prevnonw=matrix(NA,nrow=n.draws)
  pp.diff.dem.prevw=matrix(NA,nrow=n.draws)
  pp.diff.rep.prevw=matrix(NA,nrow=n.draws)
  pp.diff.dem.rep.prevw=matrix(NA,nrow=n.draws)
  pp.diff.dem.rep.prevnonw=matrix(NA,nrow=n.draws)
  
  #estimate predictions
  for (j in 1:n.draws){
    xb.dem.prevw=plogis(coa.pred.dem.prevw%*%t(sim.circuit.female[j,]))
    pp.dem.prevw[j]=mean(na.omit(xb.dem.prevw))
    
    xb.dem.prevnonw=plogis(coa.pred.dem.prevnonw%*%t(sim.circuit.female[j,]))
    pp.dem.prevnonw[j]=mean(na.omit(xb.dem.prevnonw))
    
    
    xb.rep.prevw=plogis(coa.pred.rep.prevw%*%t(sim.circuit.female[j,]))
    pp.rep.prevw[j]=mean(na.omit(xb.rep.prevw))
    
    xb.rep.prevnonw=plogis(coa.pred.rep.prevnonw%*%t(sim.circuit.female[j,]))
    pp.rep.prevnonw[j]=mean(na.omit(xb.rep.prevnonw))
    
    #estimate differences
    pp.diff.dem.prevw[j]=mean(na.omit(xb.dem.prevw))-mean(na.omit(xb.dem.prevnonw))
    pp.diff.rep.prevw[j]=mean(na.omit(xb.rep.prevw))-mean(na.omit(xb.rep.prevnonw))
    pp.diff.dem.rep.prevw[j]=mean(na.omit(xb.dem.prevw))-mean(na.omit(xb.rep.prevw))
    pp.diff.dem.rep.prevnonw[j]=mean(na.omit(xb.dem.prevnonw))-mean(na.omit(xb.rep.prevnonw))
  }
  
  #put predictions into previously created matrices
  circuit.dem.prevw.mean[i]=mean(pp.dem.prevw)
  circuit.dem.prevw.hi[i]=quantile(pp.dem.prevw,0.975)
  circuit.dem.prevw.lo[i]=quantile(pp.dem.prevw,0.025)
  
  circuit.dem.nonprevw.mean[i]=mean(pp.dem.prevnonw)
  circuit.dem.nonprevw.hi[i]=quantile(pp.dem.prevnonw,0.975)
  circuit.dem.nonprevw.lo[i]=quantile(pp.dem.prevnonw,0.025)
  
  circuit.rep.prevw.mean[i]=mean(pp.rep.prevw)
  circuit.rep.prevw.hi[i]=quantile(pp.rep.prevw,0.975)
  circuit.rep.prevw.lo[i]=quantile(pp.rep.prevw,0.025)
  
  circuit.rep.nonprevw.mean[i]=mean(pp.rep.prevnonw)
  circuit.rep.nonprevw.hi[i]=quantile(pp.rep.prevnonw,0.975)
  circuit.rep.nonprevw.lo[i]=quantile(pp.rep.prevnonw,0.025)
  
  diff.circuit.dem.prevw.mean[i]=mean(pp.diff.dem.prevw)
  diff.circuit.dem.prevw.hi[i]=quantile(pp.diff.dem.prevw,0.975)
  diff.circuit.dem.prevw.lo[i]=quantile(pp.diff.dem.prevw,0.025)
  
  diff.circuit.rep.prevw.mean[i]=mean(pp.diff.rep.prevw)
  diff.circuit.rep.prevw.hi[i]=quantile(pp.diff.rep.prevw,0.975)
  diff.circuit.rep.prevw.lo[i]=quantile(pp.diff.rep.prevw,0.025)
  
  diff.circuit.dem.rep.prevw.mean[i]=mean(pp.diff.dem.rep.prevw)
  diff.circuit.dem.rep.prevw.hi[i]=quantile(pp.diff.dem.rep.prevw,0.975)
  diff.circuit.dem.rep.prevw.lo[i]=quantile(pp.diff.dem.rep.prevw,0.025)
  
  diff.circuit.dem.rep.nonprevw.mean[i]=mean(pp.diff.dem.rep.prevnonw)
  diff.circuit.dem.rep.nonprevw.hi[i]=quantile(pp.diff.dem.rep.prevnonw,0.975)
  diff.circuit.dem.rep.nonprevw.lo[i]=quantile(pp.diff.dem.rep.prevnonw,0.025)
}







##### District ####
#set up data (note: n.draws and seq.vote must be run above (circuit model) first)
sim.district.female=coef(sim(district.female_glm,n.draws))
sim.district.female=as.data.frame(sim.district.female)

data.x.district.female=subset(noms_district,select=c('female','PrevJgender','prevJswitch','DivSen','femalesenator',
                                                     'BenchPropFemale','PrevNomFail',"daysleft",'approval','PresParty','homestateDC3','congress',
                                                     'CourtStateNumeric'))
data.x.district.female=na.omit(data.x.district.female) #get rid of any NAs

#create dummy variables for fixed effects
data.x.district.female.new=dummy_cols(data.x.district.female,select_columns=c('homestateDC3','congress','CourtStateNumeric'))
data.x.district.female.new=data.x.district.female.new[,-c(11:14,17,38)] #remove duplicate columns


#set up prediction matrices
district.dem.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
district.dem.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
district.dem.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

district.dem.nonprevw.mean=as.matrix(NA,nrow=length(seq.vote))
district.dem.nonprevw.hi=as.matrix(NA,nrow=length(seq.vote))
district.dem.nonprevw.lo=as.matrix(NA,nrow=length(seq.vote))

district.rep.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
district.rep.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
district.rep.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

district.rep.nonprevw.mean=as.matrix(NA,nrow=length(seq.vote))
district.rep.nonprevw.hi=as.matrix(NA,nrow=length(seq.vote))
district.rep.nonprevw.lo=as.matrix(NA,nrow=length(seq.vote))

diff.district.dem.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
diff.district.dem.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
diff.district.dem.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

diff.district.rep.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
diff.district.rep.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
diff.district.rep.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

diff.district.dem.rep.prevw.mean=as.matrix(NA,nrow=length(seq.vote))
diff.district.dem.rep.prevw.hi=as.matrix(NA,nrow=length(seq.vote))
diff.district.dem.rep.prevw.lo=as.matrix(NA,nrow=length(seq.vote))

diff.district.dem.rep.nonprevw.mean=as.matrix(NA,nrow=length(seq.vote))
diff.district.dem.rep.nonprevw.hi=as.matrix(NA,nrow=length(seq.vote))
diff.district.dem.rep.nonprevw.lo=as.matrix(NA,nrow=length(seq.vote))



for(i in 1:length(seq.vote)){
  district.pred.dem.prevw=cbind(1,
                                1, #judge prev woman
                                data.x.district.female.new$prevJswitch,
                                data.x.district.female.new$DivSen,
                                data.x.district.female.new$femalesenator,
                                data.x.district.female.new[,11:12], #home state senator FEs
                                data.x.district.female.new$BenchPropFemale,
                                data.x.district.female.new$PrevNomFail,
                                data.x.district.female.new$daysleft,
                                data.x.district.female.new$approval,
                                0, #dempres
                                data.x.district.female.new[,13:32], #congress FEs
                                data.x.district.female.new[,33:81], #state FEs 
                                0 #prev woman dempres int
  )
  district.pred.dem.prevw=as.matrix(district.pred.dem.prevw)
  
  district.pred.dem.prevnonw=cbind(1,
                                   0, #judge prev man
                                   data.x.district.female.new$prevJswitch,
                                   data.x.district.female.new$DivSen,
                                   data.x.district.female.new$femalesenator,
                                   data.x.district.female.new[,11:12], #home state senator FEs
                                   data.x.district.female.new$BenchPropFemale,
                                   data.x.district.female.new$PrevNomFail,
                                   data.x.district.female.new$daysleft,
                                   data.x.district.female.new$approval,
                                   0, #dempres
                                   data.x.district.female.new[,13:32], #congress FEs
                                   data.x.district.female.new[,33:81], #state FEs 
                                   0 #prev man dempres int 
  )
  district.pred.dem.prevnonw=as.matrix(district.pred.dem.prevnonw)
  
  district.pred.rep.prevw=cbind(1,
                                1, #judge prev woman
                                data.x.district.female.new$prevJswitch,
                                data.x.district.female.new$DivSen,
                                data.x.district.female.new$femalesenator,
                                data.x.district.female.new[,11:12], #home state senator FEs
                                data.x.district.female.new$BenchPropFemale,
                                data.x.district.female.new$PrevNomFail,
                                data.x.district.female.new$daysleft,
                                data.x.district.female.new$approval,
                                1, #reppres
                                data.x.district.female.new[,13:32], #congress FEs
                                data.x.district.female.new[,33:81], #state FEs
                                1 #prev woman reppres int
  )
  district.pred.rep.prevw=as.matrix(district.pred.rep.prevw)
  
  district.pred.rep.prevnonw=cbind(1,
                                   0, #judge prev man
                                   data.x.district.female.new$prevJswitch,
                                   data.x.district.female.new$DivSen,
                                   data.x.district.female.new$femalesenator,
                                   data.x.district.female.new[,11:12], #home state senator FEs
                                   data.x.district.female.new$BenchPropFemale,
                                   data.x.district.female.new$PrevNomFail,
                                   data.x.district.female.new$daysleft,
                                   data.x.district.female.new$approval,
                                   1, #reppres
                                   data.x.district.female.new[,13:32], #congress FEs
                                   data.x.district.female.new[,33:81], #state FEs
                                   0 #prev man reppres int
  )
  district.pred.rep.prevnonw=as.matrix(district.pred.rep.prevnonw)
  
  #set up predicted probability matrices
  pp.district.dem.prevw=matrix(NA,nrow=n.draws)
  pp.district.dem.prevnonw=matrix(NA,nrow=n.draws)
  pp.district.rep.prevw=matrix(NA,nrow=n.draws)
  pp.district.rep.prevnonw=matrix(NA,nrow=n.draws)
  pp.district.diff.dem.prevw=matrix(NA,nrow=n.draws)
  pp.district.diff.rep.prevw=matrix(NA,nrow=n.draws)
  pp.district.diff.dem.rep.prevw=matrix(NA,nrow=n.draws)
  pp.district.diff.dem.rep.prevnonw=matrix(NA,nrow=n.draws)
  
  #estimate predictions
  for (j in 1:n.draws){
    xb.district.dem.prevw=plogis(district.pred.dem.prevw%*%t(sim.district.female[j,]))
    pp.district.dem.prevw[j]=mean(na.omit(xb.district.dem.prevw))
    
    xb.district.dem.prevnonw=plogis(district.pred.dem.prevnonw%*%t(sim.district.female[j,]))
    pp.district.dem.prevnonw[j]=mean(na.omit(xb.district.dem.prevnonw))
    
    
    xb.district.rep.prevw=plogis(district.pred.rep.prevw%*%t(sim.district.female[j,]))
    pp.district.rep.prevw[j]=mean(na.omit(xb.district.rep.prevw))
    
    xb.district.rep.prevnonw=plogis(district.pred.rep.prevnonw%*%t(sim.district.female[j,]))
    pp.district.rep.prevnonw[j]=mean(na.omit(xb.district.rep.prevnonw))
    
    #estimate differences
    pp.district.diff.dem.prevw[j]=mean(na.omit(xb.district.dem.prevw))-mean(na.omit(xb.district.dem.prevnonw))
    pp.district.diff.rep.prevw[j]=mean(na.omit(xb.district.rep.prevw))-mean(na.omit(xb.district.rep.prevnonw))
    pp.district.diff.dem.rep.prevw[j]=mean(na.omit(xb.district.dem.prevw))-mean(na.omit(xb.district.rep.prevw))
    pp.district.diff.dem.rep.prevnonw[j]=mean(na.omit(xb.district.dem.prevnonw))-mean(na.omit(xb.district.rep.prevnonw))
  }
  
  #put predictions into previously created matrices
  district.dem.prevw.mean[i]=mean(pp.district.dem.prevw)
  district.dem.prevw.hi[i]=quantile(pp.district.dem.prevw,0.975)
  district.dem.prevw.lo[i]=quantile(pp.district.dem.prevw,0.025)
  
  district.dem.nonprevw.mean[i]=mean(pp.district.dem.prevnonw)
  district.dem.nonprevw.hi[i]=quantile(pp.district.dem.prevnonw,0.975)
  district.dem.nonprevw.lo[i]=quantile(pp.district.dem.prevnonw,0.025)
  
  district.rep.prevw.mean[i]=mean(pp.district.rep.prevw)
  district.rep.prevw.hi[i]=quantile(pp.district.rep.prevw,0.975)
  district.rep.prevw.lo[i]=quantile(pp.district.rep.prevw,0.025)
  
  district.rep.nonprevw.mean[i]=mean(pp.district.rep.prevnonw)
  district.rep.nonprevw.hi[i]=quantile(pp.district.rep.prevnonw,0.975)
  district.rep.nonprevw.lo[i]=quantile(pp.district.rep.prevnonw,0.025)
  
  diff.district.dem.prevw.mean[i]=mean(pp.district.diff.dem.prevw)
  diff.district.dem.prevw.hi[i]=quantile(pp.district.diff.dem.prevw,0.975)
  diff.district.dem.prevw.lo[i]=quantile(pp.district.diff.dem.prevw,0.025)
  
  diff.district.rep.prevw.mean[i]=mean(pp.district.diff.rep.prevw)
  diff.district.rep.prevw.hi[i]=quantile(pp.district.diff.rep.prevw,0.975)
  diff.district.rep.prevw.lo[i]=quantile(pp.district.diff.rep.prevw,0.025)
  
  diff.district.dem.rep.prevw.mean[i]=mean(pp.district.diff.dem.rep.prevw)
  diff.district.dem.rep.prevw.hi[i]=quantile(pp.district.diff.dem.rep.prevw,0.975)
  diff.district.dem.rep.prevw.lo[i]=quantile(pp.district.diff.dem.rep.prevw,0.025)
  
  diff.district.dem.rep.nonprevw.mean[i]=mean(pp.district.diff.dem.rep.prevnonw)
  diff.district.dem.rep.nonprevw.hi[i]=quantile(pp.district.diff.dem.rep.prevnonw,0.975)
  diff.district.dem.rep.nonprevw.lo[i]=quantile(pp.district.diff.dem.rep.prevnonw,0.025)
}







#################################
######### Female Senator
#################################
##### Circuit ####
circuit.femalesenator.mean=as.matrix(NA,nrow=length(seq.vote))
circuit.femalesenator.hi=as.matrix(NA,nrow=length(seq.vote))
circuit.femalesenator.lo=as.matrix(NA,nrow=length(seq.vote))

circuit.malesenator.mean=as.matrix(NA,nrow=length(seq.vote))
circuit.malesenator.hi=as.matrix(NA,nrow=length(seq.vote))
circuit.malesenator.lo=as.matrix(NA,nrow=length(seq.vote))

diff.circuit.sengen.mean=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.sengen.hi=as.matrix(NA,nrow=length(seq.vote))
diff.circuit.sengen.lo=as.matrix(NA,nrow=length(seq.vote))




for(i in 1:length(seq.vote)){
  coa.pred.malesenator=cbind(1,
                             data.x.circuit.female.new$PrevJgender,
                             data.x.circuit.female.new$prevJswitch,
                             data.x.circuit.female.new$DivSen,
                             0, #male senator
                             data.x.circuit.female.new[,11:12], #home state senator FEs
                             data.x.circuit.female.new$BenchPropFemale,
                             data.x.circuit.female.new$PrevNomFail,
                             data.x.circuit.female.new$daysleft,
                             data.x.circuit.female.new$PropSensFemale,
                             data.x.circuit.female.new$approval,
                             data.x.circuit.female.new$PresParty,
                             data.x.circuit.female.new[,13:32], #congress FEs
                             data.x.circuit.female.new$PrevJgender*data.x.circuit.female.new$PresParty
  )
  coa.pred.malesenator=as.matrix(coa.pred.malesenator)
  
  coa.pred.femalesenator=cbind(1,
                               data.x.circuit.female.new$PrevJgender,
                               data.x.circuit.female.new$prevJswitch,
                               data.x.circuit.female.new$DivSen,
                               1, #female senator
                               data.x.circuit.female.new[,11:12], #home state senator FEs
                               data.x.circuit.female.new$BenchPropFemale,
                               data.x.circuit.female.new$PrevNomFail,
                               data.x.circuit.female.new$daysleft,
                               data.x.circuit.female.new$PropSensFemale,
                               data.x.circuit.female.new$approval,
                               data.x.circuit.female.new$PresParty,
                               data.x.circuit.female.new[,13:32], #congress FEs
                               data.x.circuit.female.new$PrevJgender*data.x.circuit.female.new$PresParty
  )
  coa.pred.femalesenator=as.matrix(coa.pred.femalesenator)
  
  
  
  pp.circuit.malesenator=matrix(NA,nrow=n.draws)
  pp.circuit.femalesenator=matrix(NA,nrow=n.draws)
  pp.circuit.diff.sengen=matrix(NA,nrow=n.draws)
  
  for (j in 1:n.draws){
    xb.circuit.malesenator=plogis(coa.pred.malesenator%*%t(sim.circuit.female[j,]))
    pp.circuit.malesenator[j]=mean(na.omit(xb.circuit.malesenator))
    
    xb.circuit.femalesenator=plogis(coa.pred.femalesenator%*%t(sim.circuit.female[j,]))
    pp.circuit.femalesenator[j]=mean(na.omit(xb.circuit.femalesenator))
    
    pp.circuit.diff.sengen[j]=mean(na.omit(xb.circuit.femalesenator))-mean(na.omit(xb.circuit.malesenator))
  }
  
  circuit.femalesenator.mean[i]=mean(pp.circuit.femalesenator)
  circuit.femalesenator.hi[i]=quantile(pp.circuit.femalesenator,0.975)
  circuit.femalesenator.lo[i]=quantile(pp.circuit.femalesenator,0.025)
  
  circuit.malesenator.mean[i]=mean(pp.circuit.malesenator)
  circuit.malesenator.hi[i]=quantile(pp.circuit.malesenator,0.975)
  circuit.malesenator.lo[i]=quantile(pp.circuit.malesenator,0.025)
  
  diff.circuit.sengen.mean[i]=mean(pp.circuit.diff.sengen)
  diff.circuit.sengen.hi[i]=quantile(pp.circuit.diff.sengen,0.975)
  diff.circuit.sengen.lo[i]=quantile(pp.circuit.diff.sengen,0.025)
}




##### District ####
district.femalesenator.mean=as.matrix(NA,nrow=length(seq.vote))
district.femalesenator.hi=as.matrix(NA,nrow=length(seq.vote))
district.femalesenator.lo=as.matrix(NA,nrow=length(seq.vote))

district.malesenator.mean=as.matrix(NA,nrow=length(seq.vote))
district.malesenator.hi=as.matrix(NA,nrow=length(seq.vote))
district.malesenator.lo=as.matrix(NA,nrow=length(seq.vote))

diff.district.sengen.mean=as.matrix(NA,nrow=length(seq.vote))
diff.district.sengen.hi=as.matrix(NA,nrow=length(seq.vote))
diff.district.sengen.lo=as.matrix(NA,nrow=length(seq.vote))




for(i in 1:length(seq.vote)){
  coa.pred.malesenator=cbind(1,
                             data.x.district.female.new$PrevJgender,
                             data.x.district.female.new$prevJswitch,
                             data.x.district.female.new$DivSen,
                             0, #male senator
                             data.x.district.female.new[,11:12], #home state senator FEs
                             data.x.district.female.new$BenchPropFemale,
                             data.x.district.female.new$PrevNomFail,
                             data.x.district.female.new$daysleft,
                             data.x.district.female.new$approval,
                             data.x.district.female.new$PresParty,
                             data.x.district.female.new[,13:32], #congress FEs
                             data.x.district.female.new[,33:81], #state FEs 
                             data.x.district.female.new$PrevJgender*data.x.district.female.new$PresParty
  )
  coa.pred.malesenator=as.matrix(coa.pred.malesenator)
  
  coa.pred.femalesenator=cbind(1,
                               data.x.district.female.new$PrevJgender,
                               data.x.district.female.new$prevJswitch,
                               data.x.district.female.new$DivSen,
                               1, #female senator
                               data.x.district.female.new[,11:12], #home state senator FEs
                               data.x.district.female.new$BenchPropFemale,
                               data.x.district.female.new$PrevNomFail,
                               data.x.district.female.new$daysleft,
                               data.x.district.female.new$approval,
                               data.x.district.female.new$PresParty,
                               data.x.district.female.new[,13:32], #congress FEs
                               data.x.district.female.new[,33:81], #state FEs
                               data.x.district.female.new$PrevJgender*data.x.district.female.new$PresParty
  )
  coa.pred.femalesenator=as.matrix(coa.pred.femalesenator)
  
  
  
  pp.district.malesenator=matrix(NA,nrow=n.draws)
  pp.district.femalesenator=matrix(NA,nrow=n.draws)
  pp.district.diff.sengen=matrix(NA,nrow=n.draws)
  
  for (j in 1:n.draws){
    xb.district.malesenator=plogis(coa.pred.malesenator%*%t(sim.district.female[j,]))
    pp.district.malesenator[j]=mean(na.omit(xb.district.malesenator))
    
    xb.district.femalesenator=plogis(coa.pred.femalesenator%*%t(sim.district.female[j,]))
    pp.district.femalesenator[j]=mean(na.omit(xb.district.femalesenator))
    
    pp.district.diff.sengen[j]=mean(na.omit(xb.district.femalesenator))-mean(na.omit(xb.district.malesenator))
  }
  
  district.femalesenator.mean[i]=mean(pp.district.femalesenator)
  district.femalesenator.hi[i]=quantile(pp.district.femalesenator,0.975)
  district.femalesenator.lo[i]=quantile(pp.district.femalesenator,0.025)
  
  district.malesenator.mean[i]=mean(pp.district.malesenator)
  district.malesenator.hi[i]=quantile(pp.district.malesenator,0.975)
  district.malesenator.lo[i]=quantile(pp.district.malesenator,0.025)
  
  diff.district.sengen.mean[i]=mean(pp.district.diff.sengen)
  diff.district.sengen.hi[i]=quantile(pp.district.diff.sengen,0.975)
  diff.district.sengen.lo[i]=quantile(pp.district.diff.sengen,0.025)
}





#################################
######### Success
#################################
##### Circuit ####
sim.circuit.success=coef(sim(circuit.female.success,n.draws))
sim.circuit.success=as.data.frame(sim.circuit.success)

data.x.circuit.success=subset(noms_circuit,select=c('female','PrevJgender','prevJswitch','DivSen','femalesenator',
                                                    'BenchPropFemale','PrevNomFail',"daysleft",'PropSensFemale','approval','PresParty','homestateDC3','congress',
                                                    'CourtStateNumeric','ABAnum','election'))
data.x.circuit.success=na.omit(data.x.circuit.success) 


#create dummy variables for fixed effects
data.x.circuit.success.female.new=dummy_cols(data.x.circuit.success,select_columns=c('homestateDC3','congress','CourtStateNumeric','ABAnum'))
data.x.circuit.success.female.new=data.x.circuit.success.female.new[,-c(12:15,17,20,41,91)] #remove duplicate columns



success.circuit.dem.female.mean=as.matrix(NA,nrow=length(seq.vote))
success.circuit.dem.female.hi=as.matrix(NA,nrow=length(seq.vote))
success.circuit.dem.female.lo=as.matrix(NA,nrow=length(seq.vote))

success.circuit.dem.male.mean=as.matrix(NA,nrow=length(seq.vote))
success.circuit.dem.male.hi=as.matrix(NA,nrow=length(seq.vote))
success.circuit.dem.male.lo=as.matrix(NA,nrow=length(seq.vote))

success.circuit.rep.female.mean=as.matrix(NA,nrow=length(seq.vote))
success.circuit.rep.female.hi=as.matrix(NA,nrow=length(seq.vote))
success.circuit.rep.female.lo=as.matrix(NA,nrow=length(seq.vote))

success.circuit.rep.male.mean=as.matrix(NA,nrow=length(seq.vote))
success.circuit.rep.male.hi=as.matrix(NA,nrow=length(seq.vote))
success.circuit.rep.male.lo=as.matrix(NA,nrow=length(seq.vote))

diff.success.circuit.dem.female.mean=as.matrix(NA,nrow=length(seq.vote))
diff.success.circuit.dem.female.hi=as.matrix(NA,nrow=length(seq.vote))
diff.success.circuit.dem.female.lo=as.matrix(NA,nrow=length(seq.vote))

diff.success.circuit.rep.female.mean=as.matrix(NA,nrow=length(seq.vote))
diff.success.circuit.rep.female.hi=as.matrix(NA,nrow=length(seq.vote))
diff.success.circuit.rep.female.lo=as.matrix(NA,nrow=length(seq.vote))

diff.success.circuit.dem.rep.female.mean=as.matrix(NA,nrow=length(seq.vote))
diff.success.circuit.dem.rep.female.hi=as.matrix(NA,nrow=length(seq.vote))
diff.success.circuit.dem.rep.female.lo=as.matrix(NA,nrow=length(seq.vote))

diff.success.circuit.dem.rep.male.mean=as.matrix(NA,nrow=length(seq.vote))
diff.success.circuit.dem.rep.male.hi=as.matrix(NA,nrow=length(seq.vote))
diff.success.circuit.dem.rep.male.lo=as.matrix(NA,nrow=length(seq.vote))



for(i in 1:length(seq.vote)){
  circuit.success.pred.dem.female=cbind(1,
                                        1, #female nom
                                        data.x.circuit.success.female.new$PrevJgender,
                                        data.x.circuit.success.female.new$prevJswitch,
                                        data.x.circuit.success.female.new$DivSen,
                                        data.x.circuit.success.female.new$femalesenator,
                                        data.x.circuit.success.female.new[,13:14], #home state senator FEs
                                        data.x.circuit.success.female.new[,84:85], #aba score
                                        data.x.circuit.success.female.new$BenchPropFemale,
                                        data.x.circuit.success.female.new$PrevNomFail,
                                        data.x.circuit.success.female.new$daysleft,
                                        data.x.circuit.success.female.new$PropSensFemale,
                                        data.x.circuit.success.female.new$approval,
                                        data.x.circuit.success.female.new$election,
                                        0, #dempres
                                        data.x.circuit.success.female.new[,15:34], #congress FEs
                                        data.x.circuit.success.female.new[,35:83], #state FEs
                                        0 #prev woman dempres int
  )
  circuit.success.pred.dem.female=as.matrix(circuit.success.pred.dem.female)
  
  circuit.success.pred.dem.male=cbind(1,
                                      0, #male nom
                                      data.x.circuit.success.female.new$PrevJgender,
                                      data.x.circuit.success.female.new$prevJswitch,
                                      data.x.circuit.success.female.new$DivSen,
                                      data.x.circuit.success.female.new$femalesenator,
                                      data.x.circuit.success.female.new[,13:14], #home state senator FEs
                                      data.x.circuit.success.female.new[,84:85], #aba score
                                      data.x.circuit.success.female.new$BenchPropFemale,
                                      data.x.circuit.success.female.new$PrevNomFail,
                                      data.x.circuit.success.female.new$daysleft,
                                      data.x.circuit.success.female.new$PropSensFemale,
                                      data.x.circuit.success.female.new$approval,
                                      data.x.circuit.success.female.new$election,
                                      0, #dempres
                                      data.x.circuit.success.female.new[,15:34], #congress FEs
                                      data.x.circuit.success.female.new[,35:83],
                                      0 #prev man dempres int
  )
  circuit.success.pred.dem.male=as.matrix(circuit.success.pred.dem.male)
  
  circuit.success.pred.rep.female=cbind(1,
                                        1, #female nom
                                        data.x.circuit.success.female.new$PrevJgender,
                                        data.x.circuit.success.female.new$prevJswitch,
                                        data.x.circuit.success.female.new$DivSen,
                                        data.x.circuit.success.female.new$femalesenator,
                                        data.x.circuit.success.female.new[,13:14], #home state senator FEs
                                        data.x.circuit.success.female.new[,84:85], #aba score
                                        data.x.circuit.success.female.new$BenchPropFemale,
                                        data.x.circuit.success.female.new$PrevNomFail,
                                        data.x.circuit.success.female.new$daysleft,
                                        data.x.circuit.success.female.new$PropSensFemale,
                                        data.x.circuit.success.female.new$approval,
                                        data.x.circuit.success.female.new$election,
                                        1, #reppres
                                        data.x.circuit.success.female.new[,15:34], #congress FEs
                                        data.x.circuit.success.female.new[,35:83], 
                                        1 #prev woman reppres int
  )
  circuit.success.pred.rep.female=as.matrix(circuit.success.pred.rep.female)
  
  circuit.success.pred.rep.male=cbind(1,
                                      0, #female nom
                                      data.x.circuit.success.female.new$PrevJgender,
                                      data.x.circuit.success.female.new$prevJswitch,
                                      data.x.circuit.success.female.new$DivSen,
                                      data.x.circuit.success.female.new$femalesenator,
                                      data.x.circuit.success.female.new[,13:14], #home state senator FEs
                                      data.x.circuit.success.female.new[,84:85], #aba score
                                      data.x.circuit.success.female.new$BenchPropFemale,
                                      data.x.circuit.success.female.new$PrevNomFail,
                                      data.x.circuit.success.female.new$daysleft,
                                      data.x.circuit.success.female.new$PropSensFemale,
                                      data.x.circuit.success.female.new$approval,
                                      data.x.circuit.success.female.new$election,
                                      1, #reppres
                                      data.x.circuit.success.female.new[,15:34], #congress FEs
                                      data.x.circuit.success.female.new[,35:83], 
                                      0 #prev man reppres int
  )
  circuit.success.pred.rep.male=as.matrix(circuit.success.pred.rep.male)
  
  pp.success.circuit.dem.female=matrix(NA,nrow=n.draws)
  pp.success.circuit.dem.male=matrix(NA,nrow=n.draws)
  pp.success.circuit.rep.female=matrix(NA,nrow=n.draws)
  pp.success.circuit.rep.male=matrix(NA,nrow=n.draws)
  pp.success.circuit.diff.dem.female=matrix(NA,nrow=n.draws)
  pp.success.circuit.diff.rep.female=matrix(NA,nrow=n.draws)
  pp.success.circuit.diff.dem.rep.female=matrix(NA,nrow=n.draws)
  pp.success.circuit.diff.dem.rep.male=matrix(NA,nrow=n.draws)
  
  
  for (j in 1:n.draws){
    xb.success.circuit.dem.female=plogis(circuit.success.pred.dem.female%*%t(sim.circuit.success[j,]))
    pp.success.circuit.dem.female[j]=mean(na.omit(xb.success.circuit.dem.female))
    
    xb.success.circuit.dem.male=plogis(circuit.success.pred.dem.male%*%t(sim.circuit.success[j,]))
    pp.success.circuit.dem.male[j]=mean(na.omit(xb.success.circuit.dem.male))
    
    
    xb.success.circuit.rep.female=plogis(circuit.success.pred.rep.female%*%t(sim.circuit.success[j,]))
    pp.success.circuit.rep.female[j]=mean(na.omit(xb.success.circuit.rep.female))
    
    xb.success.circuit.rep.male=plogis(circuit.success.pred.rep.male%*%t(sim.circuit.success[j,]))
    pp.success.circuit.rep.male[j]=mean(na.omit(xb.success.circuit.rep.male))
    
    pp.success.circuit.diff.dem.female[j]=mean(na.omit(xb.success.circuit.dem.female))-mean(na.omit(xb.success.circuit.dem.male))
    pp.success.circuit.diff.rep.female[j]=mean(na.omit(xb.success.circuit.rep.female))-mean(na.omit(xb.success.circuit.rep.male))
    pp.success.circuit.diff.dem.rep.female[j]=mean(na.omit(xb.success.circuit.dem.female))-mean(na.omit(xb.success.circuit.rep.female))
    pp.success.circuit.diff.dem.rep.male[j]=mean(na.omit(xb.success.circuit.dem.male))-mean(na.omit(xb.success.circuit.rep.male))
  }
  
  
  success.circuit.dem.female.mean[i]=mean(pp.success.circuit.dem.female)
  success.circuit.dem.female.hi[i]=quantile(pp.success.circuit.dem.female,0.917)
  success.circuit.dem.female.lo[i]=quantile(pp.success.circuit.dem.female,0.083)
  
  success.circuit.dem.male.mean[i]=mean(pp.success.circuit.dem.male)
  success.circuit.dem.male.hi[i]=quantile(pp.success.circuit.dem.male,0.917)
  success.circuit.dem.male.lo[i]=quantile(pp.success.circuit.dem.male,0.083)
  
  success.circuit.rep.female.mean[i]=mean(pp.success.circuit.rep.female)
  success.circuit.rep.female.hi[i]=quantile(pp.success.circuit.rep.female,0.917)
  success.circuit.rep.female.lo[i]=quantile(pp.success.circuit.rep.female,0.083)
  
  success.circuit.rep.male.mean[i]=mean(pp.success.circuit.rep.male)
  success.circuit.rep.male.hi[i]=quantile(pp.success.circuit.rep.male,0.917)
  success.circuit.rep.male.lo[i]=quantile(pp.success.circuit.rep.male,0.083)
  
  diff.success.circuit.dem.female.mean[i]=mean(pp.success.circuit.diff.dem.female)
  diff.success.circuit.dem.female.hi[i]=quantile(pp.success.circuit.diff.dem.female,0.975)
  diff.success.circuit.dem.female.lo[i]=quantile(pp.success.circuit.diff.dem.female,0.025)
  
  diff.success.circuit.rep.female.mean[i]=mean(pp.success.circuit.diff.rep.female)
  diff.success.circuit.rep.female.hi[i]=quantile(pp.success.circuit.diff.rep.female,0.975)
  diff.success.circuit.rep.female.lo[i]=quantile(pp.success.circuit.diff.rep.female,0.025)
  
  diff.success.circuit.dem.rep.female.mean[i]=mean(pp.success.circuit.diff.dem.rep.female)
  diff.success.circuit.dem.rep.female.hi[i]=quantile(pp.success.circuit.diff.dem.rep.female,0.975)
  diff.success.circuit.dem.rep.female.lo[i]=quantile(pp.success.circuit.diff.dem.rep.female,0.025)
  
  diff.success.circuit.dem.rep.male.mean[i]=mean(pp.success.circuit.diff.dem.rep.male)
  diff.success.circuit.dem.rep.male.hi[i]=quantile(pp.success.circuit.diff.dem.rep.male,0.975)
  diff.success.circuit.dem.rep.male.lo[i]=quantile(pp.success.circuit.diff.dem.rep.male,0.025)
}


##### District ####
sim.district.success=coef(sim(district.female.sucess,n.draws))
sim.district.success=as.data.frame(sim.district.success)

data.x.district.success=subset(noms_district,select=c('female','PrevJgender','prevJswitch','DivSen','femalesenator',
                                                      'BenchPropFemale','PrevNomFail',"daysleft",'approval','PresParty','homestateDC3','congress',
                                                      'CourtStateNumeric','ABAnum','election'))
data.x.district.success=na.omit(data.x.district.success) #get rid of any NAs


data.x.district.success.female.new=dummy_cols(data.x.district.success,select_columns=c('homestateDC3','congress','CourtStateNumeric','ABAnum'))
data.x.district.success.female.new=data.x.district.success.female.new[,-c(11:14,16,19,40,90)] #remove duplicate columns

success.district.dem.female.mean=as.matrix(NA,nrow=length(seq.vote))
success.district.dem.female.hi=as.matrix(NA,nrow=length(seq.vote))
success.district.dem.female.lo=as.matrix(NA,nrow=length(seq.vote))

success.district.dem.male.mean=as.matrix(NA,nrow=length(seq.vote))
success.district.dem.male.hi=as.matrix(NA,nrow=length(seq.vote))
success.district.dem.male.lo=as.matrix(NA,nrow=length(seq.vote))

success.district.rep.female.mean=as.matrix(NA,nrow=length(seq.vote))
success.district.rep.female.hi=as.matrix(NA,nrow=length(seq.vote))
success.district.rep.female.lo=as.matrix(NA,nrow=length(seq.vote))

success.district.rep.male.mean=as.matrix(NA,nrow=length(seq.vote))
success.district.rep.male.hi=as.matrix(NA,nrow=length(seq.vote))
success.district.rep.male.lo=as.matrix(NA,nrow=length(seq.vote))

diff.success.district.dem.female.mean=as.matrix(NA,nrow=length(seq.vote))
diff.success.district.dem.female.hi=as.matrix(NA,nrow=length(seq.vote))
diff.success.district.dem.female.lo=as.matrix(NA,nrow=length(seq.vote))

diff.success.district.rep.female.mean=as.matrix(NA,nrow=length(seq.vote))
diff.success.district.rep.female.hi=as.matrix(NA,nrow=length(seq.vote))
diff.success.district.rep.female.lo=as.matrix(NA,nrow=length(seq.vote))

diff.success.district.dem.rep.female.mean=as.matrix(NA,nrow=length(seq.vote))
diff.success.district.dem.rep.female.hi=as.matrix(NA,nrow=length(seq.vote))
diff.success.district.dem.rep.female.lo=as.matrix(NA,nrow=length(seq.vote))

diff.success.district.dem.rep.male.mean=as.matrix(NA,nrow=length(seq.vote))
diff.success.district.dem.rep.male.hi=as.matrix(NA,nrow=length(seq.vote))
diff.success.district.dem.rep.male.lo=as.matrix(NA,nrow=length(seq.vote))



for(i in 1:length(seq.vote)){
  district.success.pred.dem.female=cbind(1,
                                         1, #female nom
                                         data.x.circuit.success.female.new$PrevJgender,
                                         data.x.circuit.success.female.new$prevJswitch,
                                         data.x.circuit.success.female.new$DivSen,
                                         data.x.circuit.success.female.new$femalesenator,
                                         data.x.circuit.success.female.new[,12:13], #home state senator FEs
                                         data.x.circuit.success.female.new[,83:84], #aba score
                                         data.x.circuit.success.female.new$BenchPropFemale,
                                         data.x.circuit.success.female.new$PrevNomFail,
                                         data.x.circuit.success.female.new$daysleft,
                                         data.x.circuit.success.female.new$approval,
                                         data.x.circuit.success.female.new$election,
                                         0, #dempres
                                         data.x.circuit.success.female.new[,14:33], #congress FEs
                                         data.x.circuit.success.female.new[,34:82], #state FEs
                                         0 #prev woman dempres int
  )
  district.success.pred.dem.female=as.matrix(district.success.pred.dem.female)
  
  district.success.pred.dem.male=cbind(1,
                                       0, #female nom
                                       data.x.circuit.success.female.new$PrevJgender,
                                       data.x.circuit.success.female.new$prevJswitch,
                                       data.x.circuit.success.female.new$DivSen,
                                       data.x.circuit.success.female.new$femalesenator,
                                       data.x.circuit.success.female.new[,12:13], #home state senator FEs
                                       data.x.circuit.success.female.new[,83:84], #aba score
                                       data.x.circuit.success.female.new$BenchPropFemale,
                                       data.x.circuit.success.female.new$PrevNomFail,
                                       data.x.circuit.success.female.new$daysleft,
                                       data.x.circuit.success.female.new$approval,
                                       data.x.circuit.success.female.new$election,
                                       0, #dempres
                                       data.x.circuit.success.female.new[,14:33], #congress FEs
                                       data.x.circuit.success.female.new[,34:82], #state FEs 
                                       0 #prev man dempres int
  )
  district.success.pred.dem.male=as.matrix(district.success.pred.dem.male)
  
  district.success.pred.rep.female=cbind(1,
                                         1, #female nom
                                         data.x.circuit.success.female.new$PrevJgender,
                                         data.x.circuit.success.female.new$prevJswitch,
                                         data.x.circuit.success.female.new$DivSen,
                                         data.x.circuit.success.female.new$femalesenator,
                                         data.x.circuit.success.female.new[,12:13], #home state senator FEs
                                         data.x.circuit.success.female.new[,83:84], #aba score
                                         data.x.circuit.success.female.new$BenchPropFemale,
                                         data.x.circuit.success.female.new$PrevNomFail,
                                         data.x.circuit.success.female.new$daysleft,
                                         data.x.circuit.success.female.new$approval,
                                         data.x.circuit.success.female.new$election,
                                         1, #reppres
                                         data.x.circuit.success.female.new[,14:33], #congress FEs
                                         data.x.circuit.success.female.new[,34:82], 
                                         1 #prev woman reppres int
  )
  district.success.pred.rep.female=as.matrix(district.success.pred.rep.female)
  
  district.success.pred.rep.male=cbind(1,
                                       0, #male nom
                                       data.x.circuit.success.female.new$PrevJgender,
                                       data.x.circuit.success.female.new$prevJswitch,
                                       data.x.circuit.success.female.new$DivSen,
                                       data.x.circuit.success.female.new$femalesenator,
                                       data.x.circuit.success.female.new[,12:13], #home state senator FEs
                                       data.x.circuit.success.female.new[,83:84], #aba score
                                       data.x.circuit.success.female.new$BenchPropFemale,
                                       data.x.circuit.success.female.new$PrevNomFail,
                                       data.x.circuit.success.female.new$daysleft,
                                       data.x.circuit.success.female.new$approval,
                                       data.x.circuit.success.female.new$election,
                                       1, #reppres
                                       data.x.circuit.success.female.new[,14:33], #congress FEs
                                       data.x.circuit.success.female.new[,34:82], 
                                       0 #prev man reppres int
  )
  district.success.pred.rep.male=as.matrix(district.success.pred.rep.male)
  
  pp.success.district.dem.female=matrix(NA,nrow=n.draws)
  pp.success.district.dem.male=matrix(NA,nrow=n.draws)
  pp.success.district.rep.female=matrix(NA,nrow=n.draws)
  pp.success.district.rep.male=matrix(NA,nrow=n.draws)
  pp.success.district.diff.dem.female=matrix(NA,nrow=n.draws)
  pp.success.district.diff.rep.female=matrix(NA,nrow=n.draws)
  pp.success.district.diff.dem.rep.female=matrix(NA,nrow=n.draws)
  pp.success.district.diff.dem.rep.male=matrix(NA,nrow=n.draws)
  
  
  for (j in 1:n.draws){
    xb.success.district.dem.female=plogis(district.success.pred.dem.female%*%t(sim.district.success[j,]))
    pp.success.district.dem.female[j]=mean(na.omit(xb.success.district.dem.female))
    
    xb.success.district.dem.male=plogis(district.success.pred.dem.male%*%t(sim.district.success[j,]))
    pp.success.district.dem.male[j]=mean(na.omit(xb.success.district.dem.male))
    
    
    xb.success.district.rep.female=plogis(district.success.pred.rep.female%*%t(sim.district.success[j,]))
    pp.success.district.rep.female[j]=mean(na.omit(xb.success.district.rep.female))
    
    xb.success.district.rep.male=plogis(district.success.pred.rep.male%*%t(sim.district.success[j,]))
    pp.success.district.rep.male[j]=mean(na.omit(xb.success.district.rep.male))
    
    pp.success.district.diff.dem.female[j]=mean(na.omit(xb.success.district.dem.female))-mean(na.omit(xb.success.district.dem.male))
    pp.success.district.diff.rep.female[j]=mean(na.omit(xb.success.district.rep.female))-mean(na.omit(xb.success.district.rep.male))
    pp.success.district.diff.dem.rep.female[j]=mean(na.omit(xb.success.district.dem.female))-mean(na.omit(xb.success.district.rep.female))
    pp.success.district.diff.dem.rep.male[j]=mean(na.omit(xb.success.district.dem.male))-mean(na.omit(xb.success.district.rep.male))
  }
  
  
  success.district.dem.female.mean[i]=mean(pp.success.district.dem.female)
  success.district.dem.female.hi[i]=quantile(pp.success.district.dem.female,0.917)
  success.district.dem.female.lo[i]=quantile(pp.success.district.dem.female,0.083)
  
  success.district.dem.male.mean[i]=mean(pp.success.district.dem.male)
  success.district.dem.male.hi[i]=quantile(pp.success.district.dem.male,0.917)
  success.district.dem.male.lo[i]=quantile(pp.success.district.dem.male,0.083)
  
  success.district.rep.female.mean[i]=mean(pp.success.district.rep.female)
  success.district.rep.female.hi[i]=quantile(pp.success.district.rep.female,0.917)
  success.district.rep.female.lo[i]=quantile(pp.success.district.rep.female,0.083)
  
  success.district.rep.male.mean[i]=mean(pp.success.district.rep.male)
  success.district.rep.male.hi[i]=quantile(pp.success.district.rep.male,0.917)
  success.district.rep.male.lo[i]=quantile(pp.success.district.rep.male,0.083)
  
  diff.success.district.dem.female.mean[i]=mean(pp.success.district.diff.dem.female)
  diff.success.district.dem.female.hi[i]=quantile(pp.success.district.diff.dem.female,0.975)
  diff.success.district.dem.female.lo[i]=quantile(pp.success.district.diff.dem.female,0.025)
  
  diff.success.district.rep.female.mean[i]=mean(pp.success.district.diff.rep.female)
  diff.success.district.rep.female.hi[i]=quantile(pp.success.district.diff.rep.female,0.975)
  diff.success.district.rep.female.lo[i]=quantile(pp.success.district.diff.rep.female,0.025)
  
  diff.success.district.dem.rep.female.mean[i]=mean(pp.success.district.diff.dem.rep.female)
  diff.success.district.dem.rep.female.hi[i]=quantile(pp.success.district.diff.dem.rep.female,0.975)
  diff.success.district.dem.rep.female.lo[i]=quantile(pp.success.district.diff.dem.rep.female,0.025)
  
  diff.success.district.dem.rep.male.mean[i]=mean(pp.success.district.diff.dem.rep.male)
  diff.success.district.dem.rep.male.hi[i]=quantile(pp.success.district.diff.dem.rep.male,0.975)
  diff.success.district.dem.rep.male.lo[i]=quantile(pp.success.district.diff.dem.rep.male,0.025)
}





###################################################################################################################
###################################################################################################################
###################################################################################################################
###################################################################################################################
#########
######### Figures
#########
###################################################################################################################
###################################################################################################################
###################################################################################################################
###################################################################################################################

######################################
######### Female Nominee -- Main Model
######################################

##### Circuit ####
##### Predicted Values ####
plot(NULL,ylim=c(0,0.82),xlim=c(0.5,5.5),main='',ylab='Likelihood of Female Nominee',xlab='',xaxt='n',yaxt='n')
axis(1,at=c(1.5,4.5),c('Democratic Presidents','Republican Presidents'),tick=F)
axis(2,cex.axis=0.8)
points(1,circuit.dem.prevw.mean[1],pch=16)
segments(1,circuit.dem.prevw.hi,1,circuit.dem.prevw.lo)
points(2,circuit.dem.nonprevw.mean[1],pch=21)
segments(2,circuit.dem.nonprevw.hi,2,circuit.dem.nonprevw.lo)
points(4,circuit.rep.prevw.mean[1],pch=16)
segments(4,circuit.rep.prevw.hi,4,circuit.rep.prevw.lo)
points(5,circuit.rep.nonprevw.mean[1],pch=21)
segments(5,circuit.rep.nonprevw.hi,5,circuit.rep.nonprevw.lo)
legend(0.25,0.085,pch=c(16,21),c('Prev Judge Female','Prev Judge Male'),cex=0.65,y.intersp=0.75,bty='n')




##### First Differences ####
par(mar=c(5.1,5.1,4.1,2.1))
plot(NULL, ylim=c(-0.55,0.65),xlim=c(0.5,4.5),main='',ylab='First Difference \n (Circuit Nominees)',xlab='',xaxt='n',yaxt='n')
axis(1,at=c(1:4),c('Dem Prev Female - \n Dem Prev Male','Rep Prev Female - \n Rep Prev Male', 'Dem Prev Female - \n Rep Prev Female',
                   'Dem Prev Male - \n Rep Prev Male'),tick=F,cex.axis=0.5)
axis(2,cex.axis=0.6)
abline(h=0,lty=2)
points(1,diff.circuit.dem.prevw.mean[1],pch=16)
segments(1,diff.circuit.dem.prevw.hi,1,diff.circuit.dem.prevw.lo)
points(2,diff.circuit.rep.prevw.mean[1],pch=16)
segments(2,diff.circuit.rep.prevw.hi,2,diff.circuit.rep.prevw.lo)
points(3,diff.circuit.dem.rep.prevw.mean[1],pch=16)
segments(3,diff.circuit.dem.rep.prevw.hi,3,diff.circuit.dem.rep.prevw.lo)
points(4,diff.circuit.dem.rep.nonprevw.mean[1],pch=16)
segments(4,diff.circuit.dem.rep.nonprevw.hi,4,diff.circuit.dem.rep.nonprevw.lo)




##### District ####
##### Predicted Values ####
plot(NULL,ylim=c(0.05,0.75),xlim=c(0.5,5.5),main='',ylab='Likelihood of Female Nominee',xlab='',xaxt='n')
axis(1,at=c(1.5,4.5),c('Democratic Presidents','Republican Presidents'),tick=F)
points(1,district.dem.prevw.mean[1],pch=16)
segments(1,district.dem.prevw.hi,1,district.dem.prevw.lo)
points(2,district.dem.nonprevw.mean[1],pch=21)
segments(2,district.dem.nonprevw.hi,2,district.dem.nonprevw.lo)
points(4,district.rep.prevw.mean[1],pch=16)
segments(4,district.rep.prevw.hi,4,district.rep.prevw.lo)
points(5,district.rep.nonprevw.mean[1],pch=21)
segments(5,district.rep.nonprevw.hi,5,district.rep.nonprevw.lo)
legend(0.25,0.15,pch=c(16,21),c('Prev Judge Female','Prev Judge Male'),cex=0.65,y.intersp=0.75,bty='n')


##### First Differences ####
par(mar=c(5.1,5.1,4.1,2.1))
plot(NULL, ylim=c(-0.46,0.63),xlim=c(0.5,4.5),main='',ylab='First Difference \n (District Nominees)',xlab='',xaxt='n',yaxt='n')
axis(1,at=c(1:4),c('Dem Prev Female - \n Dem Prev Male','Rep Prev Female - \n Rep Prev Male', 'Dem Prev Female - \n Rep Prev Female',
                   'Dem Prev Male - \n Rep Prev Male'),tick=F,cex.axis=0.5)
axis(2,cex.axis=0.6)
abline(h=0,lty=2)
points(1,diff.district.dem.prevw.mean[1],pch=16)
segments(1,diff.district.dem.prevw.hi,1,diff.district.dem.prevw.lo)
points(2,diff.district.rep.prevw.mean[1],pch=16)
segments(2,diff.district.rep.prevw.hi,2,diff.district.rep.prevw.lo)
points(3,diff.district.dem.rep.prevw.mean[1],pch=16)
segments(3,diff.district.dem.rep.prevw.hi,3,diff.district.dem.rep.prevw.lo)
points(4,diff.district.dem.rep.nonprevw.mean[1],pch=16)
segments(4,diff.district.dem.rep.nonprevw.hi,4,diff.district.dem.rep.nonprevw.lo)




#################################
######### Female Senators
#################################
##### Circuit ####
##### Predicted Values ####
plot(NULL,ylim=c(0.2,0.5),xlim=c(0.5,2.5),main='',ylab='Likelihood of Female Nominee',xlab='',xaxt='n')
axis(1,at=c(1,2),c('Male Senators','Female Senator(s)'),tick=F)
points(1,circuit.malesenator.mean[1],pch=16)
segments(1,circuit.malesenator.hi,1,circuit.malesenator.lo)
points(2,circuit.femalesenator.mean[1],pch=16)
segments(2,circuit.femalesenator.hi,2,circuit.femalesenator.lo)



##### District ####
##### Predicted Values ####
plot(NULL,ylim=c(0.2,0.4),xlim=c(0.5,2.5),main='',ylab='Likelihood of Female Nominee',xlab='',xaxt='n',yaxt='n')
axis(1,at=c(1,2),c('Male Senators','Female Senator(s)'),tick=F)
axis(2,cex.axis=0.6)
points(1,district.malesenator.mean[1],pch=16)
segments(1,district.malesenator.hi,1,district.malesenator.lo)
points(2,district.femalesenator.mean[1],pch=16)
segments(2,district.femalesenator.hi,2,district.femalesenator.lo)




##### First Differences (Combined Plot District and Circuit) ####
par(mar=c(5.1,5.1,4.1,2.1))
plot(NULL, ylim=c(-0.05,0.17),xlim=c(0.5,2.5),main='',ylab='First Difference \n (Female - Male Senator)',xlab='',xaxt='n')
axis(1,at=c(1,2),c('Circuit','District'),tick=F)
#axis(2,cex.axis=0.6)
abline(h=0,lty=2)
points(1,diff.circuit.sengen.mean[1],pch=16)
segments(1,diff.circuit.sengen.hi,1,diff.circuit.sengen.lo)
points(2,diff.district.sengen.mean[1],pch=16)
segments(2,diff.district.sengen.hi,2,diff.district.sengen.lo)





#################################
######### Success
#################################
##### Circuit ####
##### First Differences ####
par(mar=c(5.1,5.1,4.1,2.1))
plot(NULL, ylim=c(-0.15,0.1),xlim=c(0.5,2.5),main='',ylab='First Difference: Nomination Success \n (Circuit Nominees)',xlab='',xaxt='n',yaxt='n')
axis(1,at=c(1:2),c('Dem Female - \n Dem Male','Rep Female - \n Rep Male'),tick=F,cex.axis=.75)
axis(2,cex.axis=0.5)
abline(h=0,lty=2)
points(1,diff.success.circuit.dem.female.mean[1],pch=16)
segments(1,diff.success.circuit.dem.female.hi,1,diff.success.circuit.dem.female.lo)
points(2,diff.success.circuit.rep.female.mean[1],pch=16)
segments(2,diff.success.circuit.rep.female.hi,2,diff.success.circuit.rep.female.lo)


##### District ####
par(mar=c(5.1,5.1,4.1,2.1))
plot(NULL, ylim=c(-0.05,0.1),xlim=c(0.5,2.5),main='',ylab='First Difference: Nomination Success \n (District Nominees)',xlab='',xaxt='n',yaxt='n')
axis(1,at=c(1:2),c('Dem Female - \n Dem Male','Rep Female - \n Rep Male'),tick=F,cex.axis=0.75)
axis(2,cex.axis=0.6)
abline(h=0,lty=2)
points(1,diff.success.district.dem.female.mean[1],pch=16)
segments(1,diff.success.district.dem.female.hi,1,diff.success.district.dem.female.lo)
points(2,diff.success.district.rep.female.mean[1],pch=16)
segments(2,diff.success.district.rep.female.hi,2,diff.success.district.rep.female.lo)
























